{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 一键获取数据\n",
    "!cd work && bash run/onekey.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 一键训练、量化任务\n",
    "!cd work && bash run/onetasks.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# PaddleX 图像分类模型\n",
    "# 'PPLCNet', 'PPLCNet_ssld', 'ResNet18', 'ResNet18_vd', 'ResNet34', \n",
    "# 'ResNet34_vd', 'ResNet50', 'ResNet50_vd', 'ResNet50_vd_ssld', 'ResNet101', \n",
    "# 'ResNet101_vd', 'ResNet101_vd_ssld', 'ResNet152', 'ResNet152_vd', 'ResNet200_vd', \n",
    "# 'DarkNet53', 'MobileNetV1', 'MobileNetV2', 'MobileNetV3_small', 'MobileNetV3_small_ssld', \n",
    "# 'MobileNetV3_large', 'MobileNetV3_large_ssld', 'Xception41', 'Xception65', 'Xception71', \n",
    "# 'ShuffleNetV2', 'ShuffleNetV2_swish', 'DenseNet121', 'DenseNet161', 'DenseNet169', \n",
    "# 'DenseNet201', 'DenseNet264', 'HRNet_W18_C', 'HRNet_W30_C', 'HRNet_W32_C', \n",
    "# 'HRNet_W40_C', 'HRNet_W44_C', 'HRNet_W48_C', 'HRNet_W64_C', 'AlexNet'\n",
    "# --lr_decay_epochs \"32\" --lr_decay_gamma 1 \\\n",
    "# --save_interval_epochs 4 \\\n",
    "# --pretrain_weights ./output/best_model/model.pdparams \\\n",
    "# 训练 --num_workers 2 \"IMAGENET\"\n",
    "!cd work && python3 run/train.py \\\n",
    "    --dataset ./dataset/train \\\n",
    "    --epochs 32 \\\n",
    "    --batch_size 16 \\\n",
    "    --learning_rate 0.01 \\\n",
    "    --lr_decay_epochs \"16\"\\\n",
    "    --lr_decay_gamma 0.25 \\\n",
    "    --model MobileNetV3_large_ssld \\\n",
    "    --pretrain_weights \"IMAGENET\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# --save_interval_epochs 4 \\\n",
    "# 裁剪 --model_dir ./output/best_model --save_dir ./output/prune\n",
    "!cd work && python3 run/prune.py \\\n",
    "    --dataset ./dataset/train \\\n",
    "    --epochs 16 \\\n",
    "    --batch_size 16 \\\n",
    "    --learning_rate 0.001 \\\n",
    "    --lr_decay_epochs \"8\"\\\n",
    "    --lr_decay_gamma 0.25 \\\n",
    "    --model_dir ./output/best_model \\\n",
    "    --save_dir ./output/prune \\\n",
    "    --pruned_flops 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# --save_interval_epochs 4 \\\n",
    "# 在线量化 --model_dir ./output/best_model --save_dir ./output/quant\n",
    "!cd work && python3 run/quant.py \\\n",
    "    --dataset ./dataset/train \\\n",
    "    --epochs 16 \\\n",
    "    --batch_size 16 \\\n",
    "    --learning_rate 0.001 \\\n",
    "    --lr_decay_epochs \"8\"\\\n",
    "    --lr_decay_gamma 0.25 \\\n",
    "    --model_dir ./output/best_model \\\n",
    "    --save_dir ./output/quant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试\n",
    "!cd work && python3 run/test.py --model_dir ./output/best_model \\\n",
    "    --epochs 4 \\\n",
    "    --dataset ./dataset/train \\\n",
    "    --test_list ./dataset/train/test_list.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T15:16:33.802623Z",
     "iopub.status.busy": "2022-05-25T15:16:33.801796Z",
     "iopub.status.idle": "2022-05-25T15:16:34.829316Z",
     "shell.execute_reply": "2022-05-25T15:16:34.828256Z",
     "shell.execute_reply.started": "2022-05-25T15:16:33.802580Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 压缩模型\n",
    "!mkdir work/bak\n",
    "!cd work && tar -caf ./bak/MobileNetV3_large_ssld.tar.gz ./output/best_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 清除数据集\n",
    "!rm -r work/dataset/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 清除模型参数\n",
    "!rm -r work/output/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 查看支持的模型\n",
    "!cd work && python3 run/train.py --model_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 解压代码\n",
    "!unzip -oq run.zip -d ./work/"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
